package com.yohann.educenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.yohann.commonutils.jwt.JwtUtils;
import com.yohann.educenter.entity.UcenterMember;
import com.yohann.educenter.service.UcenterMemberService;
import com.yohann.educenter.utils.HttpClientUtils;
import com.yohann.educenter.utils.WeChatPropertiesUtil;
import com.yohann.servicebase.exception.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author Yohann
 * @since 2020/8/15
 */
@Controller
@RequestMapping("/api/ucenter/wx")
public class WeChatApiController {

    @Autowired
    private WeChatPropertiesUtil weChatPropertiesUtil;
    @Autowired
    private UcenterMemberService memberService;

    /**
     * 生成微信登录二维码
     * @return
     */
    @GetMapping("login")
    public String getQrCode(){
        // 微信开放平台授权baseUrl
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
        "?appid=%s" +
        "&redirect_uri=%s" +
        "&response_type=code" +
        "&scope=snsapi_login" +
        "&state=%s" +
        "#wechat_redirect";

        // 回调地址
        String redirectUrl = weChatPropertiesUtil.getRedirectUri();
        try {
            //url编码
            redirectUrl = URLEncoder.encode(redirectUrl, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new GuliException(20001, e.getMessage());
        }

        // 设置参数
        String qrcodeUrl = String.format(
                baseUrl,
                weChatPropertiesUtil.getAppId(),
                redirectUrl,
                "Yohann");

        return "redirect:" + qrcodeUrl;
    }

    /**
     * 扫码成功获取信息
     * @param code
     * @param state
     * @return
     */
    @GetMapping("callback")
    public String callback(String code, String state){
        try {
            //1.获取code值，临时票据，类似于验证码
            //2.拿着code请求，微信固定的地址，得到两个值 access_token 和openId
            String baseAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token" +
                    "?appid=%s" +
                    "&secret=%s" +
                    "&code=%s" +
                    "&grant_type=authorization_code";

            //拼接三个参数 ：id  秘钥 和 code值
            String accessTokenUrl = String.format(
                    baseAccessTokenUrl,
                    weChatPropertiesUtil.getAppId(),
                    weChatPropertiesUtil.getAppSecret(),
                    code
                );

            //请求这个拼接好的地址，得到返回两个值 access_token 和 openId
            //使用httpclient发送请求，得到返回结果
            String accessTokenInfo = HttpClientUtils.get(accessTokenUrl);

            //从accessTokenInfo 字符串出来两个值 accessToken 和 openId
            //把accessTokenInfo 字符串转换成map集合，根据map里面可以 获取对应的值
            //使用json转换工具
            Gson gson = new Gson();
            HashMap<String,String> accessTokenMap = gson.fromJson(accessTokenInfo, HashMap.class);

            String accessToken = accessTokenMap.get("access_token");
            String openid = accessTokenMap.get("openid");

            //把扫描人信息添加到数据库里面
            //判断数据库表里面是否存在相同微信信息，根据openid判断
            QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("openid",openid);
            UcenterMember member = memberService.getOne(queryWrapper);

            if(Objects.isNull(member)){
                //3.拿着得到access_token和openid ,再去请求微信提供的固定地址，获取到扫描人信息
                // 访问微信的资源服务器，获取到用户信息

                String baseUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo" +
                        "?access_token=%s" +
                        "&openid=%s";
                //拼接两个参数
                String userInfoUrl = String.format(
                        baseUserInfoUrl,
                        accessToken,
                        openid
                );

                //发送请求
                String userInfo = HttpClientUtils.get(userInfoUrl);

                //获取返回的userInfo信息
                HashMap<String, String> userInfoMap = gson.fromJson(userInfo, HashMap.class);

                //昵称
                String nickname = userInfoMap.get("nickname");
                //头像
                String headimgurl = userInfoMap.get("headimgurl");

                member = new UcenterMember();
                member.setOpenid(openid);
                member.setNickname(nickname);
                member.setAvatar(headimgurl);
                memberService.save(member);
        }

        //使用jwt根据member对象生成token字符串
            String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

            //最后: 返回首页面，通过路径传递token字符串

            return "redirect:http://localhost:3000?token="+token;

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001,e.getMessage());
        }
    }
}